/**
 * 
 */
package com.dsteiner.widgets.client.util;

import java.util.Date;

import com.dsteiner.widgets.client.DateUtilImpl;
import com.dsteiner.widgets.client.IDateUtil;
import com.dsteiner.widgets.client.ObjectUtil;
import com.dsteiner.widgets.client.util.ValidationUtil.Error;

public interface Validator<T>{
	public static final Validator<String> EMAIL_VALIDATOR = new Validator<String>(){
		public Error validate(String... input) {
			assert input.length == 1;
			ValidationResults validationResults = new ValidationResults();
			String email = input[0];
			if(email == null)
				validationResults.add(new ValidationError("ingrese su email", "email"));
			else{
				boolean isValid = email.matches("^([a-zA-Z0-9_\\.\\-])+\\@(([a-zA-Z0-9\\-])+\\.)+([a-zA-Z0-9]{2,4})+$");
				if(!isValid)
					validationResults.add(new ValidationError("ingrese una direccion electronica valida", "email"));
			}
			return validationResults; 
		}
	};
	public static final Validator<Object> NOT_NULL_VALIDATOR = new Validator<Object>(){
		public Error validate(Object... input) {
			assert input.length == 3;
			return input[0] != null ? null : new ValidationError(input[1].toString(), input[2].toString());
		}
	};
	public class BirthdayValidator implements Validator<String>{
		private IDateUtil dateUtil;
		public Error validate(String... input) {
			assert input.length == 1;
			String birthday = input[0];
			ValidationResults validationResults = new ValidationResults();
			if(birthday == null){
				validationResults.add(new ValidationError("ingrese su cumpleaños", "birthday"));
			}
			else{
				Date birthdayDate = getDateUtil().parseDateSpanish(birthday);
				if(birthdayDate == null){
					validationResults.add(new ValidationError("ingrese su cumpleaños en el formato dd/mm/aaaa", "birthday"));
				}
				else if(getDateUtil().getAge(birthdayDate) < 12 || getDateUtil().getAge(birthdayDate) > 99){
					validationResults.add(new ValidationError("su edad no puede ser menos de 12 y mas de 99", "birthday"));
				}
			}
			return validationResults;
		}
		private IDateUtil getDateUtil() {
			return dateUtil != null ? dateUtil : DateUtilImpl.getInstance();
		}
		public void setDateUtil(IDateUtil dateUtil){
			this.dateUtil = dateUtil;
		}
	}
	public static final BirthdayValidator BIRTHDAY_VALIDATOR = new BirthdayValidator();
	public static final Validator<String> PSWD_VALIDATOR = new Validator<String>(){
		public Error validate(String... input) {
			assert input.length == 2;
			String password = input[0];
			String passwordConfirm = input[1];
			ValidationResults validationResults = new ValidationResults();
			if(!ObjectUtil.equals(passwordConfirm, password)){
				validationResults.add(new ValidationError("contraseñas no son iguales", "password", "passwordConfirm"));
			}
			if(password == null){
				validationResults.add(new ValidationError("ingrese su contraseña", "password"));
			}
			if(password != null && password.length() < 6){
				validationResults.add(new ValidationError("ingrese su contraseña con al menos 6 letras", "password"));
			}
			return validationResults;
		}
	};

	public Error validate(T... input);
}